package org.esprit.ocm.server.metier.ec2.impl;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.server.metier.ec2.ConsoleService;

import com.xerox.amazonws.ec2.ConsoleOutput;
import com.xerox.amazonws.ec2.Jec2;

public class ConsoleServiceImpl extends BaseServiceImpl implements ConsoleService {

	/**
	 * Get console ouput for a particular instance.
	 * 
	 * @param instanceId
	 *            The instanceId of the instance to get the console output from.
	 * @return ConsoleOutput object containing output information or null in
	 *         case of error.
	 */
	public ConsoleOutput getConsoleOutput(String instanceId, ServerDto server, AwsCredentialsDto credentials) {

		// initialize the interface
		Jec2 ec2 = this.initConnection(server, credentials);

		try {

			return ec2.getConsoleOutput(instanceId);

		} catch (Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.log(Level.SEVERE, "Retrieving console output for instance " + instanceId + " failed: " + ex.getMessage());

			return null;
		}

	}
}
